Skip to content

Resolve array~list #4225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open

Conversation

VincentLanglet
Copy link
Contributor

Closes phpstan/phpstan#9734

I'll need your decision on the condition to chose in ArrayType::tryRemove @ondrejmirtes

We could have

if ($typeToRemove->isSuperTypeOf(new ConstantArrayType([], []))->yes()) {
     return TypeCombinator::intersect($this, new NonEmptyArrayType());
}

but it means that now a

@phpstan-assert-if-true string[] $array

will infer a non empty array for the false case.

It could make sens since in order to say that all the values are not string, it require to access to at least one value.

If preferred we could restrict such behavior to the list with an extra $typeToRemove->isList()->yes() check.

Or, to just solve array_is_list ; the check can be something like $typeToRemove->describe(...) === 'list'...

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test somewhere which asserts the types for array_is_list(mixed)?

@VincentLanglet
Copy link
Contributor Author

Do we have a test somewhere which asserts the types for array_is_list(mixed)?

Dunno, but what do you have in mind ?
It gives

/**
	 * @param mixed $a
	 * @return void
	 */
	public function doFoo4($a): void
	{
		if (array_is_list($a)) {
			assertType('list', $a);
		} else {
			assertType('mixed~list', $a);
		}
	}

I added a test.

@staabm
Copy link
Contributor

staabm commented Aug 9, 2025

Looks good - just making sure we have it covered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

array is non-empty when array_is_list returns false
2 participants